package proyectoia;

import aima.search.framework.Successor;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Brian Genera el conjunt de successors obtinguts canviant una petició
 * de servidor o intercanviant dos peticions.
 */
public class DistFileSystemSuccessorFunction2 extends DistFileSystemSuccessorFunction {

    DistFileSystemSuccessorFunction2() {
        super();
    }

    @Override
    public List getSuccessors(Object s) {
        super.getSuccessors(s);
        ArrayList sucs = new ArrayList();
        DistFileSystemState state = (DistFileSystemState) s;
        DistFileSystemState newState;
        for (Integer s1 = 0; s1 < state.numServers; s1++) {
            for (Integer req1 : state.allocations.get(s1)) {
                int file = state.getFile(req1);
                for (Integer s2 : state.getFileLocations(file)) {
                    if (s1 != s2) {
                        newState = new DistFileSystemState(state);
                        String operation = newState.changeAllocationOperator(req1, s1, s2);
                        sucs.add(new Successor(operation, newState));
                    }
                    for (Integer req2 : state.allocations.get(s2)) {
                        if (s1 < s2 && state.isAllocatable(req2, s1)) {
                            newState = new DistFileSystemState(state);
                            String operation = newState.swapAllocationsOperator(req1, req2, s1, s2);
                            sucs.add(new Successor(operation, newState));
                        }
                    }
                }
            }
        }
        return sucs;
    }
}
